﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Interface
{
    /// <summary>
    /// Interaction logic for RegistrarNuevoCliente.xaml
    /// </summary>
    public partial class RegistrarNuevoCliente : UserControl
    {
        public RegistrarNuevoCliente()
        {
            InitializeComponent();
        }

        private void UserControl_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            

        }

        private void txbxCI_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                Proceso.GestorCliente.VerificarCI(txbxCI.Text);
                lblErrorCi.Content = " ";
            }
            catch (Exception ex)
            {

                lblErrorCi.Content = ex.Message;
            }

        }

        private void txbxNombre_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                Proceso.GestorCliente.VerificarNombre(txbxNombre.Text);
                lblErrorNombre.Content = " ";
            }
            catch (Exception ex)
            {

                lblErrorNombre.Content = ex.Message;
            }
        }

        private void txbxDireccion_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                Proceso.GestorCliente.VerificarDireccion(txbxDireccion.Text);
                btnMapa.IsEnabled = true;
                lblErrorGeneral.Content = " ";
                 
            }
            catch (Exception es)
            {
                lblErrorGeneral.Content = es.Message;
            }
            

        }

        private void txbxTelefono_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                Proceso.GestorCliente.VerificarTelefono(txbxTelefono.Text);
                lblErrorTelf.Content = " ";
            }
            catch (Exception es)
            {
                lblErrorTelf.Content = es.Message;
            }
            
        }

        private void btnRegistrar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Proceso.GestorCliente.InsertarCliente(txbxCI.Text, txbxTelefono.Text, txbxNombre.Text, txbxDireccion.Text);
                lblErrorGeneral.Content = " ";
                MessageBox.Show("Cliente Registrado satisfactoriamente");
                txbxCI.Text = "";
                txbxDireccion.Text = "";
                txbxNombre.Text =  "";
                txbxTelefono.Text = String.Empty;
            }
            catch (Exception ex)
            {

                lblErrorGeneral.Content = ex.Message;
                if (String.IsNullOrEmpty(txbxCI.Text))
                    lblErrorCi.Content = "*";
                if (String.IsNullOrEmpty(txbxNombre.Text))
                    lblErrorNombre.Content = "*";
            }
        }

        private void btnMapa_Click(object sender, RoutedEventArgs e)
        {
            String []palabras=SeparateWords(txbxDireccion.Text);
            Process.Start("https://maps.google.com.bo/maps?hl=es&q="+palabras[0]+"+"+palabras[1]+"&ie=UTF-8");
        }
        public static String[] SeparateWords(String sentence)
        {
            String[] words = null;
            if (!String.IsNullOrEmpty(sentence))
            {
                words = sentence.Split(' ');
            }
            return words;
        }
    }
}
